package com.sikaryofficial.common.core.annotation;

import cn.hutool.core.util.NumberUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
import java.util.Objects;

/**
 * @author : qinjinyuan
 * @desc : 自定义反序列化器,格式化时间
 * @date : 2023/12/14 9:53
 */
@Slf4j
@JsonComponent
public class DoubleSerializer extends JsonSerializer<Double> {
    /**
     * @param aDouble
     * @param jsonGenerator
     * @param serializerProvider
     * @throws IOException
     */
    @Override
    public void serialize(Double aDouble, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(Objects.isNull(aDouble)){
            return;
        }
        jsonGenerator.writeString(NumberUtil.round(aDouble, 1).toString());
    }
}
